package org.yty.travelagencymongodb.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.yty.travelagencymongodb.model.Entity.Result;
import org.yty.travelagencymongodb.model.Entity.Traveller;
import org.yty.travelagencymongodb.service.TravellerService;

@RestController
@RequestMapping("/traveller")
@RequiredArgsConstructor
public class TravellerController {
    private final TravellerService travellerService;

    @GetMapping("/{id}")
    public Result<Traveller> getTravellerById(@PathVariable Long id) {
        Traveller result = travellerService.getById(id);
        if (result != null) {
            return Result.ok(result);
        } else {
            return Result.error("用户不存在");
        }
    }

    @PostMapping
    public Result<Void> saveTraveller(@RequestBody Traveller traveller){
        boolean success = this.travellerService.save(traveller);
        if (success){
            return Result.ok();
        }
        return Result.error("插入失败");
    }

    @PutMapping
    public Result<Void> updateTraveller(@RequestBody Traveller traveller){
        boolean success = this.travellerService.update(traveller);
        if (success){
            return Result.ok();
        }
        return Result.error("更新失败");
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteTravellerById(@PathVariable Long id){
        this.travellerService.removeById(id);
        return Result.ok();
    }
}
